package org.mspring.mlog.web.freemarker.directive;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.mspring.mlog.ServiceFactory;
import org.mspring.mlog.entity.Stat;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.directive.AbstractDirectiveModel;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * @author Gao Youbo
 * @since 2013年12月28日
 */
public class StatDirectiveModel extends AbstractDirectiveModel {

    @Override
    public void exec(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        // TODO Auto-generated method stub
        String key = MapUtils.getString(params, "key");
        if (StringUtils.isBlank(key)) {
            return;
        }
        int value = 0;
        switch (key) {
        case Stat.Type.POST_COUNT:
            value = ServiceFactory.getStatService().getPostCount();
            break;
        case Stat.Type.COMMENT_COUNT:
            value = ServiceFactory.getStatService().getReplys();
            break;
        case Stat.Type.CLICK_COUNT:
            value = ServiceFactory.getStatService().getClickCount();
            break;
        }
        env.getOut().append(String.valueOf(value));
    }
}
